﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Collections;

namespace BYManageInfoSystem
{
    public partial class Frm_Home2 : DockContent
    {
        TableLayoutPanelCellPosition pos;
        private Dictionary<string, string> m_dicModuleName = new Dictionary<string, string>();
        private int m_nNum = 3;
        public Frm_Home2()
        {
            InitializeComponent();
            //InitializeVideo(49);
        }

        public Frm_Home2(Dictionary<string, string> dicModuleName, string strTitle)
        {
            InitializeComponent();
            //m_dicModuleName = dicModuleName;
            foreach (string strTemp in dicModuleName.Keys)
            {
                m_dicModuleName.Add(strTemp, dicModuleName[strTemp]);
            }
            //foreach (string strTemp in dicModuleName.Keys)
            //{
            //    m_dicModuleName.Add(strTemp + "1", dicModuleName[strTemp]);
            //}

            m_nNum = (int)Math.Ceiling(Math.Sqrt(m_dicModuleName.Count));
            if (m_nNum <= 2)
                m_nNum = 3;
            InitializeVideoEx(m_nNum, m_dicModuleName);
         
            this.Text = strTitle;
        }
        private void InitializeVideoEx(int n, Dictionary<string, string> dicModuleName)
        {
            int nNeed = (n * 2 - 1);
            InitializeVideo(nNeed, dicModuleName);
        }

        private bool IsShouldEmpty(int nNums, int nRowIndex)
        {
            for (int i = 0; i < nNums; i++)
            {
                if (i % 2 == 1)
                {
                    if (nRowIndex / nNums == i) //控制的行
                        //|| nRowIndex % nNums == i)//控制的列
                        return true;
                }

            }
            return false;
        }

        private bool InitializeVideo(int videoNum, Dictionary<string, string> dicModuleName)
        {
            float fTotal = 100 / videoNum;
            float fButton = fTotal * 0.8f ;
            float fLabel = fTotal - fButton ;
            ArrayList al = new ArrayList();
            foreach(string strTemp in dicModuleName.Keys)
            {
                al.Add(strTemp);
            }

            int nCount = al.Count;
            //if (nCount > m_nNum * m_nNum)
            //    nCount = m_nNum * m_nNum;
            //计算行列
            int rowcol = videoNum;
            if (videoNum <= 0 )
            {
                return false;
            }
            //计算宽高
            int nWidth = (int)(tableLayoutPanel1.Width / rowcol);
            int nHeight = (int)(tableLayoutPanel1.Height / rowcol);
            //重新设置表格
            //tableLayoutPanel1为TableLayoutPanel控件
            tableLayoutPanel1.Controls.Clear();
            tableLayoutPanel1.RowCount = rowcol*2;
            tableLayoutPanel1.ColumnCount = rowcol;
            //tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tableLayoutPanel1.Refresh();
            for (int i = 0; i < tableLayoutPanel1.ColumnStyles.Count; i++)
            {
                tableLayoutPanel1.ColumnStyles[i].SizeType = SizeType.Absolute;
                tableLayoutPanel1.ColumnStyles[i].Width = nWidth;
                
            }
            for (int i = 0; i < tableLayoutPanel1.RowStyles.Count; i++)
            {
                if (i % 2 == 0)
                {
                    tableLayoutPanel1.RowStyles[i].SizeType = SizeType.Percent;
                    tableLayoutPanel1.RowStyles[i].Height = fButton;
                }
                else
                {
                    tableLayoutPanel1.RowStyles[i].SizeType = SizeType.Percent;
                    tableLayoutPanel1.RowStyles[i].Height = fLabel;
                }
       
            }

            //添加控件
            //for (int i = 0; i < videoNum; i++)
            //for (int i = 0; i < nCount; i++)
            int nIndex = 0;
            while (nCount > 0)
            {
               //空
                if (IsShouldEmpty(videoNum, nIndex))
                //if (nIndex / rowcol == 1 || nIndex / rowcol == 3 || nIndex / rowcol == 5
                //    || nIndex % rowcol == 1 || nIndex % rowcol == 3 || nIndex % rowcol == 5)
                //if (nIndex / rowcol == 1 || nIndex / rowcol == 3 || nIndex / rowcol == 5 
                //    || nIndex % rowcol == 2 || nIndex % rowcol == 3 || nIndex % rowcol == 6 || nIndex % rowcol == 7
                //    || nIndex % rowcol == 10 || nIndex % rowcol == 11)
                {
                    Console.WriteLine("@@" + nIndex + "\n");
                    nIndex++;
                    
                    continue;
                }
                ImageButton pButton = new ImageButton();
                //pVideo.Padding = pVideo.Margin = new Padding(0);
                //pVideo.Name = "pVideo" + i.ToString();

                //pVideo.Width = pVideo.Height = nHeight;
                pButton.Dock = DockStyle.Fill;
                //pButton.Text = "       " + al[nCount - 1].ToString();
                //pButton.Enabled = false;
                //pButton.Tag = m_dicModuleName[al[nCount - 1].ToString()].ToString();
                pButton.Tag = al[nCount - 1].ToString();
                pButton.NormalImage = imageList1.Images[nIndex % imageList1.Images.Count];
                pButton.SizeMode = PictureBoxSizeMode.CenterImage;
                //pVideo.BackgroundImage = Resources.bg;
                //pVideo.BackgroundImageLayout = ImageLayout.Stretch;
                pButton.Click += new System.EventHandler(pVideo_Click);
                //pVideo.Click += new EventHandler(pVideo_Click);
                tableLayoutPanel1.Controls.Add(pButton, nIndex % rowcol, nIndex / rowcol);
                Label pLabel = new Label();
                pLabel.Text = al[nCount - 1].ToString();
                pLabel.TextAlign = ContentAlignment.MiddleCenter;
                //pLabel.BackColor = Color.Aqua;
                pLabel.Anchor = AnchorStyles.Top | AnchorStyles.Bottom;
                tableLayoutPanel1.Controls.Add(pLabel, nIndex % rowcol, 1 + nIndex / rowcol);

                nIndex++;
                nCount--;
            }


            return true;
        }

        /// <summary>
        /// 点击图像事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pVideo_Click(object sender, EventArgs e)
        {
            ImageButton tsmi = (ImageButton)sender;
            Frm_Main.GetMainForm().ShowContent(tsmi.Tag.ToString());
        } 

        private void Frm_Home2_Load(object sender, EventArgs e)
        {
     
        }

        private void Frm_Home2_Resize(object sender, EventArgs e)
        {
            m_nNum = (int)Math.Ceiling(Math.Sqrt(m_dicModuleName.Count));
            if (m_nNum <= 2)
                m_nNum = 3;
            InitializeVideoEx(m_nNum, m_dicModuleName);
        }
    }
}
